<template>
  <div class="flexContent">
    <!-- 盘点管理 -->
    <div class="searchBox">
      <el-form ref="form" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="盘点名称">
                  <el-input v-clearEmoji v-model.trim="sform.name" style="width: 95%" clearable placeholder="请输入盘点名称"
                    @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="盘点编号">
                  <el-input v-clearEmoji v-model.trim="sform.number" style="width: 95%" clearable placeholder="请输入盘点编号"
                    @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="状态">
                  <el-select v-model="sform.status" clearable popper-class="eloption" placeholder="请选择状态"
                    style="width: 95%">
                    <el-option label="已盘点" :value="0"></el-option>
                    <el-option label="未盘点" :value="1"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="仓库名称">
                  <el-select v-model="sform.warehouseId" clearable filterable popper-class="eloption"
                    placeholder="请选择仓库名称" style="width: 95%" @click.native="getWarehouseList">
                    <el-option v-for="item in warehouseList" :key="item.id" :label="item.name" :value="item.id" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="10" :xl="6">
                <el-form-item label="制单时间">
                  <el-date-picker v-model="date" clearable type="daterange" unlink-panels range-separator="-"
                    start-placeholder="开始时间" end-placeholder="结束时间" value-format="yyyy-MM-dd" style="width: 95%" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetForm" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="add">
      <el-button class="addBtn" @click="toAddPage" v-has="'checkedAdd'">新增</el-button>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{resizable: true}" border class="tablebox" ref="xTable1" :row-config="{ isHover: true }"
          :scroll-y="{ enabled: true, gt:21 }" align="center" style="width: 100%" height="100%" size="medium">
          <vxe-column type="seq" title="序号" width="80" />
          <vxe-column field="number" title="盘点编号" :min-width="elTableAdaptationWidth('number', tableData)"/>
          <vxe-column field="name" title="盘点名称" :min-width="elTableAdaptationWidth('name', tableData)"/>
          <vxe-column field="warehouseName" title="仓库名称" :min-width="elTableAdaptationWidth('warehouseName', tableData)"/>
          <vxe-column field="creatorName" title="制单人" min-width="150"/>
          <vxe-column field="createTime" title="制单时间" min-width="150">
            <template v-slot="{ row }">
              <span>{{ row.createTime | parseTime }}</span>
            </template>
          </vxe-column>
          <vxe-column field="status" title="状态" width="100">
            <template v-slot="{ row }">
              <el-tag v-if="row.status === '0'" disabled type="primary" size="small">已盘点</el-tag>
              <el-tag v-if="row.status === '1'" disabled type="info" size="small">未盘点</el-tag>
            </template>
          </vxe-column>
          <vxe-column fixed="right" title="操作" width="200" v-if="$showFn(['toPrintCheck', 'to_check', 'checkDetail'])">
            <template slot-scope="scope">
              <el-button type="text" @click="printCheckInfo(scope.row.id)" v-has="'toPrintCheck'">打印</el-button>
              <el-button type="text" @click="toDetail(scope.row.id, '0')" v-has="'to_check'"
                v-if="scope.row.status == '1'">盘点</el-button>
              <el-button type="text" @click="toDetail(scope.row.id, '1')" v-has="'checkDetail'"
                v-if="scope.row.status == '0'">详情</el-button>
              <el-button type="text" style="color: red;" @click="$delFn('盘点记录', deleteCheckInfo, scope.row)"
                v-has="'del_checkInfo'" v-if="scope.row.status == '1'">删除</el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,-> , sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import { reqWarehouseList } from "@/api/procure";
import { reqCheckInfoList, reqPrintCheckInfo, reqDeleteCheckInfo } from "@/api/storage";
import { printCheckInfo, checkPrinter } from "@/utils/printLODOP"
export default {
  name: "CheckedInfo",
  data() {
    return {
      isSearch: false,
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      date: [],
      sform: {
        name: "",
        number: "",
        warehouseId: "",
        status: null,
      },
      tableData: [{ id: '12' }],


      warehouseList: [],
      printData: {},
    };
  },
  computed: {
    openTime() {
      if (this.date == null) {
        return "";
      }
      if (this.date.length > 0) {
        return this.date[0];
      } else {
        return "";
      }
    },
    endTime() {
      if (this.date == null) {
        return "";
      }
      if (this.date.length > 0) {
        return this.date[1];
      } else {
        return "";
      }
    },
  },
  created() {
    this.getCheckInfoList()
  },
  methods: {
    // 获取仓库列表
    async getWarehouseList() {
      const res = await reqWarehouseList();
      if (res.code === 200) {
        this.warehouseList = res.data;
      }
    },
    // 分页查询盘点列表
    async getCheckInfoList() {
      const { pageNum, pageSize } = this.page;
      let { number, warehouseId, name, status, } = this.sform;
      let { openTime, endTime } = this;
      if (!this.isSearch) {
        number = "";
        name = "";
        warehouseId = "";
        openTime = "";
        endTime = "";
        status = undefined;
      }
      console.log(openTime);
      const reqObj = {
        pageNum,
        pageSize,
        number,
        name,
        warehouseId,
        openTime,
        endTime,
        status,
      };
      const res = await reqCheckInfoList(reqObj);
      if (res.code === 200) {
        this.tableData = res.data.list;
        this.page.total = res.data.total;
        const $table = this.$refs.xTable1
        $table.loadData(this.tableData);
      }
    },
    resetForm(formName) {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.sform = {
        number: "",
        warehouseId: "",
        name: "",
        status: null,
      };
      this.date = [];
      this.isSearch = false;
      this.getCheckInfoList();
    },
    toAddPage() {
      this.$router.push({
        path: "checkedAdd",
      });
    },
    toDetail(id, type) {
      if (type == '0') {
        this.$router.push({
          path: "toChecked",
          query: {
            id: id,
            isDetail: type,
          },
        });
      } else {
        this.$router.push({
          path: "checkDetail",
          query: {
            id: id,
            isDetail: type,
          },
        });
      }

    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      if (this.isSearch) {
        this.searchFn();
      } else {
        this.getCheckInfoList();
      }
    },
    // 获取页数改变
    handleCurrentChange(val) {
      this.page.pageNum = val;
      if (this.isSearch) {
        this.searchFn();
      } else {
        this.getCheckInfoList();
      }
    },
    searchFn() {
      if (!this.isSearch) {
        this.page = {
          pageNum: 1,
          pageSize: 20,
          total: 0,
        };
      }
      this.isSearch = true;
      this.getCheckInfoList();
    },
    // 打印盘点单
    printCheckInfo(id) {
      this.$confirm(`即将补打盘点单，是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        if (!checkPrinter()) return;
        let res = await reqPrintCheckInfo({ id });
        if (res.code === 200) {
          this.printData = res.data;
          printCheckInfo(this.printData);
          this.$message({ type: "success", message: "正在打印盘点单" });
        }
      });
    },

    async deleteCheckInfo(row) {
      const res = await reqDeleteCheckInfo({ id: row.id });
      if (res.code === 200) {
        // 判断当前数据是不是当页最后一条，如果是页码减1
        this.$message.success("删除成功");
        if (this.tableData.length === 1 && this.page.pageNum > 1)
          this.page.pageNum--;
        this.getCheckInfoList();
      } else {
        // this.$message.error(res.message);
      }
    }
  },
};
</script>

<style lang="scss" scoped></style>
